<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <h1>Controlling which objects are pickable</h1>
    <br/>
    <p>In this example, the grey objects are set un-pickable, while the blue object remains pickable.<br><br>Try
        clicking objects to pick them.
        <br><br>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
    </ul>
</div>
</body>
<script type="module">
    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer, arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true,
    });

    viewer.camera.eye = [-3.933, 2.855, 27.018];
    viewer.camera.look = [4.400, 3.724, 8.899];
    viewer.camera.up = [-0.018, 0.999, 0.039];

    viewer.cameraControl.doublePickFlyTo = false;

    //----------------------------------------------------------------------------------------------------------------------
    // Create a xeokit loader plugin, load a model, fit to view
    //----------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(viewer);

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
        edges: true,
        pickable: false
    });

    //------------------------------------------------------------------------------------------------------------------
    // Set all objects un-pickable and grey, except for the one wall in front of camera, which we'll set pickable and blue
    //------------------------------------------------------------------------------------------------------------------

    sceneModel.on("loaded", () => {

        viewer.scene.setObjectsColorized(viewer.scene.objectIds, [0.4, 0.4, 0.4]);

        viewer.scene.setObjectsColorized(["2O2Fr$t4X7Zf8NOew3FLOH"], [0, 0, 1]);

        viewer.scene.setObjectsPickable(["2O2Fr$t4X7Zf8NOew3FLOH"], true);
    });

    //------------------------------------------------------------------------------------------------------------------
    // Click Entities to colorize them
    //------------------------------------------------------------------------------------------------------------------

    var lastEntity = null;
    var lastColorize = null;

    viewer.cameraControl.on("picked", (pickResult) => {

        console.log(pickResult.entity.id);

        if (!lastEntity || pickResult.entity.id !== lastEntity.id) {
            if (lastEntity) {
                lastEntity.colorize = lastColorize;
            }

            lastEntity = pickResult.entity;
            lastColorize = pickResult.entity.colorize.slice();

            pickResult.entity.highlighted = false;
            pickResult.entity.colorize = [1.0, 0.0, 0.0];
        }
    });

    viewer.cameraControl.on("pickedNothing", () => {
        if (lastEntity) {
            lastEntity.colorize = lastColorize;
            lastEntity = null;
        }
    });
</script>
</html>